<?php

class WebUser extends CWebUser {   
    const USER_TRAINER_ID = 'id';
    const WEB_USER_ID = 'id';

    public $loginUrl = array('/user/user/login');
    
    public function checkAccess($operation, $params = array(), $allowCaching = true) {
        return $this->hasRole($operation);
    }
    public static function hasUsers($uid = 0) {
        if ($uid == 0)
            $uid = Yii::app()->user->getId();

        $user = CActiveRecord::model('User')->findByPk($uid);

        return isset($user->users);
    }

    /**
     * Checks if this (non-admin) User can administrate the given user
     */
    public static function hasUser($username, $uid = 0) {

        if ($uid == 0)
            $uid = Yii::app()->user->getId();

        // Every user can modify himself
        if ($username == $uid)
            return true;

        $user = CActiveRecord::model('User')->findByPk($uid);

        if (!is_array($username))
            $username = array($username);

        if (isset($user->users))
            foreach ($user->users as $userobj) {
                if (in_array($userobj->username, $username) ||
                        in_array($userobj->id, $username))
                    return true;
            }
        return false;
    }

    public static function hasRole($role, $uid = 0) {
        if ($uid == 0)
            $uid = Yii::app()->user->getId();


        if (!is_array($role))
            $role = array($role);

        $user = CActiveRecord::model('User')->findByPk($uid);

        if (isset($user->roles))
            foreach ($user->roles as $roleobj) {
                if (in_array($roleobj->title, $role) ||
                        in_array($roleobj->id, $role))
                    return true;
            }
        return false;
    }

    /**
     * Return admin status.
     * @return boolean
     */
    public function isAdmin() {
        if ($this->isGuest)
            return false;
        else {
            if (User::model()->active()->superuser()->findbyPk(Yii::app()->user->id))
                return true;
            else
                return false;
        }
    }

    public function afterLogin($fromCookie) {
        $this->getLoginUser();
        parent::afterLogin($fromCookie);
    }

    public function setUsersIdInSession() {
        $session = new CHttpSession;
        $model = $this->getLoginUser();
        if ($model) {
            $session->add($attribute, $model->getAttribute($attribute));
        }
    }

    public function getLoginUser() {
        $model = false;
        $uid = Yii::app()->getUser()->getId();
        if (Yii::app()->getUser()->hasRole(SkillConfig::USER_TRAINER)) {
            $attribute = self::USER_TRAINER_ID;
            $model = Users::model()->find('id=:id', array(':id' => $uid));
        } else {
            $attribute = self::WEB_USER_ID;
            $model = Users::model()->find('id=:id', array(':id' => $uid));
        }
        return $model;
    }

    public function isAppUser() {
        if (Yii::app()->getUser()->hasRole(self::READER_ROLE_ID)) {
            return true;
        }
        return false;
    }

}

?>
